//
//  ViewController.m
//  tools
//
//  Created by dllo on 15/6/3.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
@interface ViewController ()
@property (nonatomic, retain)NSString *mypath;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.user = [NSUserDefaults standardUserDefaults];
    self.navigationController.navigationBar.translucent = NO;
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.removeFromSuperViewOnHide = YES;
    self.hud.labelText = @"加载中";
    self.hud.mode = MBProgressHUDModeIndeterminate;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
    if ([self.user boolForKey:@"name"]) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
    }else{
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];
    }
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveNoficiation:) name:@"name" object:nil];
}
- (void)receiveNoficiation:(NSNotification *)noti{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([user boolForKey:@"name"]) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
        NSLog(@"改变背景");
    }else{
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];

    }
}

- (void)connectionStr:(NSString *)str{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    self.mypath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.plist", [str hash]]];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    self.CollectStr = self.mypath;
    [self.hud show:YES];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self.hud hide:YES];
        [responseObject writeToFile:self.mypath atomically:YES];
            
        [self DataprocessingDic:responseObject];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败");
        NSLog(@"%@",error);
        [self.hud hide:YES];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:self.mypath];
        [self DataprocessingDic:dic];
    }];
    
    
}

- (void)DataprocessingDic:(id)myData{
   
    
}
-(void)dealloc{
    [super dealloc];
    [_user release];
    [_CollectStr release];
    [_hud release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
